/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.library.web_layer.readerPages;

import cz.muni.fi.pa165.library.api.BookService;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.ReaderService;
import cz.muni.fi.pa165.library.api.ReaderTO;
import cz.muni.fi.pa165.library.api.ReservationService;
import cz.muni.fi.pa165.library.api.ReservationTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.AuthenticatedSession;
import cz.muni.fi.pa165.library.web_layer.HomePage;
import cz.muni.fi.pa165.library.web_layer.Template;
import cz.muni.fi.pa165.library.web_layer.adminPages.Book.EditBook;
import cz.muni.fi.pa165.library.web_layer.adminPages.Book.ShowAllBook;
import java.sql.Timestamp;
import java.util.List;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author dulinka
 */
public class ShowAllBookReader extends Template {
    private static final long serialVersionUID = 1L; 
    private BookService bookService;
    
    public ShowAllBookReader(final PageParameters parameters) {
	super(parameters);
        
       bookService = (BookService) ApplicationContextProvider.getApplicationContext().getBean("bookService");
       RepeatingView repeating = new RepeatingView("repeating");
       add(repeating);
       
       List<BookTO> list = bookService.findAllAvailableBooks();
       
       for (final BookTO book : bookService.findAllBooks()) {
            AbstractItem item = new AbstractItem(repeating.newChildId());
            PageParameters pageParameters = new PageParameters();
            pageParameters.add("bookId", book.getId());
            if (list.contains(book)){
                Link link = new Link("edit"){
                      @Override 
                      public void onClick() {
                          ReservationService reservationService = (ReservationService) ApplicationContextProvider.getApplicationContext().getBean("reservationService");
                          AuthenticatedSession session = ((AuthenticatedSession)Session.get());
                          ReaderTO reader = session.getReader();
                          BookTO book2 = bookService.findBookById(book.getId());
                          java.util.Date date = new java.util.Date();
                          if (reader != null && book != null){
                              ReservationTO reservation = new ReservationTO(reader, book2,new Timestamp(date.getTime()));
                              reservationService.insertReservation(reservation);
                              setResponsePage(ShowAllBookReader.class);
                          }
                          
                    }  
                };
                item.add(link);
                //item.add(new BookmarkablePageLink<Void>("edit", BookReservation.class, pageParameters));
            }else {
                item.add(new Label("edit", new Model("")));
            }
            
            
            item.add(new Label("title", book.getTitle()));
            item.add(new Label("author", book.getAuthor()));
            item.add(new Label("genre", String.valueOf(book.getGenre())));
            item.add(new Label("publicationYear", String.valueOf(book.getPublicationYear())));
            item.add(new Label("publicationPlace", book.getPublicationPlace()));
            item.add(new Label("publisher", book.getPublisher()));
            item.add(new Label("ISBN", book.getISBN()));
            item.add(new Label("status", String.valueOf(book.getStatus())));
            item.add(new Label("availability", String.valueOf(book.getAvailability())));            
            repeating.add(item);
      }
        
        BookmarkablePageLink homeLink = new BookmarkablePageLink<>(
                "home", HomePage.class);
        add(homeLink);
    }
}
